package view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

/**
 * weihaizhou
 * 2018/10/18  11:41 AM
 */
public class RoundVIew extends View {

    private final String TAG = "RoundVIew";
    private Paint paint;
    private int w;
    private int h;
    private int internalRadius = 152;
    private int externalRadius = 180;
    private Context context;
    private int widthPixels;
    private int heightPixels;
    private Path path;

    public RoundVIew(Context context) {
        this(context, null);
    }


    public RoundVIew(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        paint = new Paint();
//        paint.setColor(Color.BLACK);
//        paint.setStrokeWidth(2);
//        paint.setAntiAlias(true);

//        path = new Path();

        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        widthPixels = displayMetrics.widthPixels;
        heightPixels = displayMetrics.heightPixels;

        setLayerType(LAYER_TYPE_SOFTWARE, null);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int w_mode = MeasureSpec.getMode(widthMeasureSpec);
        int wight_size = MeasureSpec.getSize(widthMeasureSpec);
        int height_size = MeasureSpec.getSize(heightMeasureSpec);
        int h_mode = MeasureSpec.getMode(heightMeasureSpec);
        if (w_mode == MeasureSpec.AT_MOST && h_mode == MeasureSpec.AT_MOST) {
            w = widthPixels;
            h = heightPixels;
        } else if (w_mode == MeasureSpec.AT_MOST && h_mode == MeasureSpec.EXACTLY) {
            w = widthPixels;
            h = height_size;
        } else if (w_mode == MeasureSpec.EXACTLY && h_mode == MeasureSpec.AT_MOST) {
            w = wight_size;
            h = heightPixels;
        } else {
            w = wight_size;
            h = height_size;
        }
        setMeasuredDimension(wight_size, height_size);

//        radius = (int) (Math.min(w, h) / 2*0.2f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(w / 2, h / 2);


//        canvas.drawCircle(0,0,internalRadius,paint);
//        paint.setColor(Color.parseColor("#b6b6b6"));
//        paint.setStyle(Paint.Style.FILL_AND_STROKE);
//        canvas.drawCircle(0,0,externalRadius,paint);
//        int line=30;
//        for (int i = 0; i <36 ; i++) {
//            canvas.drawLine(externalRadius,0,externalRadius+line,0,paint);
//            canvas.rotate(10);
//        }


//        canvas1.drawCircle(0,0,externalRadius,paint);
        Bitmap bitmap = drawArc();
//        Bitmap bitmap= makeSrc();
        canvas.drawBitmap(bitmap, 0, 0, paint);


        canvas.restore();
        Log.i(TAG, "w:" + w + " h:" + h);
    }

    private Bitmap makeSrc() {
        Bitmap bitmap = Bitmap.createBitmap(w, h , Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        c.drawRect(0,0,300,100,paint);
        return bitmap;
    }

    private Bitmap drawArc() {
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        c.translate(w / 2, h / 2);
        Path path_ = new Path();
        Paint paint_ = new Paint();
        path_.moveTo(0, 0);
        path_.lineTo(-30, externalRadius);
        path_.lineTo(-30, externalRadius + 15);
        path_.lineTo(0, externalRadius + 60);
        path_.lineTo(30, externalRadius + 15);
        path_.lineTo(30, externalRadius);
        path_.close();
        paint_.setColor(Color.RED);
        paint_.setStyle(Paint.Style.FILL);
        c.drawPath(path_, paint_);
        paint_.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        paint_.setColor(Color.parseColor("#b6b6b6"));
        paint_.setStyle(Paint.Style.FILL_AND_STROKE);
        c.drawCircle(0, 0, externalRadius, this.paint);
        paint_.setXfermode(null);
        return bitmap;
    }
}
